package com.ruoyi.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.edu.domain.EduCourseDescription;
import com.ruoyi.edu.domain.vo.EduCourseDescriptionVo;
import com.ruoyi.edu.domain.vo.EduSubjectVo;
import com.ruoyi.edu.domain.vo.EduTeacherVo;
import com.ruoyi.edu.mapper.EduCourseDescriptionMapper;
import com.ruoyi.edu.mapper.EduSubjectMapper;
import com.ruoyi.edu.mapper.EduTeacherMapper;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.mapper.SysOssMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.edu.domain.bo.EduCourseBo;
import com.ruoyi.edu.domain.vo.EduCourseVo;
import com.ruoyi.edu.domain.EduCourse;
import com.ruoyi.edu.mapper.EduCourseMapper;
import com.ruoyi.edu.service.IEduCourseService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 课程Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-18
 */
@RequiredArgsConstructor
@Service
public class EduCourseServiceImpl implements IEduCourseService {

    private final EduCourseMapper baseMapper;
    private final SysOssMapper sysOssMapper;
    private final EduTeacherMapper eduTeacherMapper;
    private final EduSubjectMapper eduSubjectMapper;
    private  final EduCourseDescriptionMapper eduCourseDescriptionMapper;

    /**
     * 查询课程
     */
    @Override
    public EduCourseVo queryById(String id){
        EduCourseVo eduCourseVo = baseMapper.selectVoById(id);
        EduTeacherVo eduTeacherVo = eduTeacherMapper.selectVoById(eduCourseVo.getTeacherId());
        eduCourseVo.setName(eduTeacherVo.getName());
        EduSubjectVo eduSubjectVo = eduSubjectMapper.selectVoById(eduCourseVo.getSubjectParentId());
        eduCourseVo.setTitleSP(eduSubjectVo.getTitle());
//        final EduSubjectVo eduSubjectVo1 = eduSubjectMapper.selectVoById(eduCourseVo.getSubjectId());
//        eduCourseVo.setTitleS(eduSubjectVo1.getTitle());
        EduCourseDescriptionVo eduCourseDescriptionVo = eduCourseDescriptionMapper.selectVoById(eduCourseVo.getId());
        eduCourseVo.setDescription(eduCourseDescriptionVo.getDescription());
        return eduCourseVo;
    }

    /**
     * 查询课程列表
     */
    @Override
    public TableDataInfo<EduCourseVo> queryPageList(EduCourseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EduCourse> lqw = buildQueryWrapper(bo);
        Page<EduCourseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<EduCourseVo> records = result.getRecords();
        List<EduCourseVo> list = queryListSetValue(records);
        return TableDataInfo.build(list,result.getTotal());
    }
    public TableDataInfo<EduCourseVo> getListByIds(List<Long> ids){
        List<EduCourseVo> list = baseMapper.selectVoList(new QueryWrapper<EduCourse>().in("id", ids));
        List<EduCourseVo> voList = queryListSetValue(list);
        return TableDataInfo.build(voList);
    }
    public List<EduCourseVo> queryListSetValue(List<EduCourseVo> records ){
        ArrayList<EduCourseVo> list = new ArrayList<>();
        for (EduCourseVo eduCourseVo:records){
            SysOssVo sysOssVo = sysOssMapper.selectVoById(eduCourseVo.getCover());
            eduCourseVo.setUrl(sysOssVo.getUrl());
            EduTeacherVo eduTeacherVo = eduTeacherMapper.selectVoById(eduCourseVo.getTeacherId());
            eduCourseVo.setName(eduTeacherVo.getName());
            EduSubjectVo eduSubjectVo = eduSubjectMapper.selectVoById(eduCourseVo.getSubjectId());
            eduCourseVo.setTitleS(eduSubjectVo.getTitle());
            EduSubjectVo eduSubjectVo1 = eduSubjectMapper.selectVoById(eduCourseVo.getSubjectParentId());
            eduCourseVo.setTitleSP(eduSubjectVo1.getTitle());
            EduCourseDescriptionVo eduCourseDescriptionVo = eduCourseDescriptionMapper.selectVoById(eduCourseVo.getId());
            if (eduCourseDescriptionVo!=null){
                eduCourseVo.setDescription(eduCourseDescriptionVo.getDescription());
            }
            list.add(eduCourseVo);
        }
        return list;
    }

    /**
     * 查询课程列表
     */
    @Override
    public List<EduCourseVo> queryList(EduCourseBo bo) {
        LambdaQueryWrapper<EduCourse> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EduCourse> buildQueryWrapper(EduCourseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EduCourse> lqw = Wrappers.lambdaQuery();
        if (bo.getIds()!=null){
            lqw.in(true, EduCourse::getId, bo.getIds());
        }
        lqw.eq(StringUtils.isNotBlank(bo.getTeacherId()), EduCourse::getTeacherId, bo.getTeacherId());
        lqw.eq(StringUtils.isNotBlank(bo.getSubjectId()), EduCourse::getSubjectId, bo.getSubjectId());
        lqw.eq(StringUtils.isNotBlank(bo.getSubjectParentId()), EduCourse::getSubjectParentId, bo.getSubjectParentId());
//        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), EduCourse::getTitle, bo.getTitle());
        lqw.like(StringUtils.isNotBlank(bo.getTitle()),EduCourse::getTitle,bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), EduCourse::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增课程
     */
    @Override
    public Boolean insertByBo(EduCourseBo bo) {
        EduCourse add = BeanUtil.toBean(bo, EduCourse.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(add.getId());
//        EduCourseDescription eduCourseDescription = eduCourseDescriptionMapper.selectById(add.getId());
        eduCourseDescription.setDescription(add.getDescription());
        boolean insert = eduCourseDescriptionMapper.insert(eduCourseDescription)>0;
        if (flag && insert) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改课程
     */
    @Override
    public Boolean updateByBo(EduCourseBo bo) {
        EduCourse update = BeanUtil.toBean(bo, EduCourse.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EduCourse entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除课程
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public R query() {
        List<EduCourseVo> eduCourseVos = baseMapper.selectVoList(null);
        return R.ok(eduCourseVos);
    }


}
